﻿package com.winmastergames.decalsystem
{
	//Import Needed Classes
	import flash.display.MovieClip;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Stage;
	import flash.geom.Matrix;
	//  decalHandler Class by Dean Gardiner
	//	1. Import Class:
	//			import com.winmastergames.decalsystem.decalHandler;
	//	2. Create Handler:
	//			var dcHandle:decalHandler = new decalHandler(<stagereference>);
	//	3. Draw Image:
	//			dcHandle.drawImage(<movieclip>, <Xpos>, <Ypos>, <bitmapDataStage>, <OptionalBitmapsArray>);
	//	4. ReDraw Bitmap Stage:
	//			dcHandle.reDraw(<bitmapDataStage>);
	public class decalHandler
	{
		//Define Variables
		public var stageReference:Stage;
		public var bitmapInit:Boolean = false;
		public var lastImage:Bitmap;
		//DecalHandler Constuctor
		public function decalHandler(stageRef:Stage)
		{
			stageReference = stageRef;
		}
		//Draw Image Method Writes Image to BitmapData stage and push's it to array if defined.
		public function drawImage (image:MovieClip, xLoc:int, yLoc:int, drawTo:BitmapData, pushToArray:Array = null)
		{
			//Transform Image
			var tempMatrix:Matrix = new Matrix(1,0,0,1,xLoc - image.x,yLoc - image.y);
			//Set Transformation
			image.transform.matrix = tempMatrix;
			//Draw Image
			drawTo.draw (image, tempMatrix);
			//Add to Array
			if (pushToArray != null)
			{
				pushToArray.push (image);
			}
		}
		//reDraw Method Gets old Child removes it and adds the new Child.
		public function reDraw (bitmapData:BitmapData)
		{
			if (bitmapInit == true)
			{
				stageReference.removeChild (lastImage);
				var newImage:Bitmap = new Bitmap(bitmapData);
				stageReference.addChild (newImage);
				lastImage = newImage;
			}
			else
			{
				var image:Bitmap = new Bitmap(bitmapData);
				stageReference.addChild (image);
				bitmapInit = true;
				lastImage = image;
			}
		}
	}
}